var util = require('../../utils/util.js')

//获取应用实例
var app = getApp()
Page({
  data: {
    item: {
      display: 'hide',
      error_info: '请将球杆还在您取杆的位置',
    },
    using_info:"hide",
    // redpacket_info:"hide",
    redpacket_count:'0',
    mask:"hide"
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  //分享
  onShareAppMessage: function () {
    return {
      title: '力杆',
      path: '/pages/index/index',
      success: function (res) {
      },
      fail: function (res) {

      }
    }
  },

  onLoad: function (option) {
    var session_id = wx.getStorageSync('session_id');
    //this.getRedPacketInfo(session_id);
    if(option.query){
      var using_ball_arm_number = wx.getStorageSync('ball_arm_number') || ''; 
      var ball_arm_number = option.query;
      //判断是还杆还是开锁取杆
      if(using_ball_arm_number == ''){//用户开锁操作
        this.unlock(ball_arm_number);
      }
      // else{
      //    if(using_ball_arm_number == ball_arm_number){
      //      this.returnBallArm(ball_arm_number);
      //    }else if(using_ball_arm_number != ball_arm_number){
      //         this.setData({
      //           item: {
      //             display: 'show',
      //             error_info: '请将球杆还在取杆的位置',
      //           },
      //         });
      //         this.closeErrorInfo();
      //    }
      // }
      
      this.setData({mask:"show"})
    } 
  },

  onShow: function () {
      this.setData({ mask: "hide" })
      var using_ball_arm_number = wx.getStorageSync('ball_arm_number') || ''; 
      if(using_ball_arm_number!= ''){
        wx.redirectTo({
           url: '/pages/beingUsed/beingUsed'
        })
      }
      //获取该用户的红包信息
      // var not_used_count = wx.getStorageSync('not_used_count');
      // if (not_used_count >= 0){
      //   this.setData({
      //     redpacket_info: "show",
      //     redpacket_count: not_used_count
      //   });
      // }      
  },

  formSubmit: function (e) {
    wx.setStorage({
      key: 'form_id',
      data: e.detail.formId 
    });
    this.doScanCode();
  },

  //获取用户的红包信息

  //用户开锁取杆
  unlock: function (ball_arm_number){
    var that = this;

    wx.showLoading({title: '正在开锁中',})
    setTimeout(function () {
      wx.hideLoading();
      that.setData({ mask: "hide" })
    }, 1000)

    wx.request({
      url: app.data.servser + '/index/unlock',
      data: {
        session_id: wx.getStorageSync('session_id'),
        ball_arm_number: ball_arm_number
      },
      success: function (res) {
        //保存扫码的结果
        wx.setStorage({key: 'scan_result',data: res.data});
        //如果取杆成功，保存二维码的信息
        if(res.data.status == 1){
           wx.setStorage({ key: 'ball_arm_number', data: ball_arm_number });
           wx.redirectTo({
             url: '/pages/beingUsed/beingUsed'
           })
        }
        //wx.navigateTo({ url: '/pages/scanresult/scanresult' });
      }
    }) 
  },
  
  //用户还杆
  // returnBallArm: function (ball_arm_number){
  //   var that = this;
  //   var session_id = wx.getStorageSync('session_id');
  //   wx.showLoading({ title: '正在还杆中', })
  //   setTimeout(function () {
  //     wx.hideLoading();
  //     that.setData({ mask: "hide" })
  //   }, 2000)

  //   wx.request({
  //     url: app.data.servser + '/index/returnBallArm',
  //     data: {
  //       session_id: session_id,
  //       ball_arm_number: ball_arm_number
  //     },
  //     success: function (res) {
  //       if(res.data.status == 1){
  //         wx.hideLoading();
  //         wx.showToast({
  //           title: '还杆成功',
  //           icon: 'success',
  //           duration: 2000
  //         });
  //          wx.removeStorageSync('scan_result');
  //          wx.removeStorageSync('ball_arm_number');
  //          that.setData({ using_info: "hide" });
  //          //去计算费用
  //          that.charge(session_id);
  //       }else{
  //         that.setData({
  //           item: {
  //             display: 'show',
  //             error_info: res.data.error_info,
  //           },
  //         });
  //         that.closeErrorInfo();
  //       }
  //     }
  //   }) 
  // },

  //计算消费
  charge:function(session_id){
    var that = this;
    var form_id = wx.getStorageSync('form_id');
    var user_feedback = wx.getStorageSync('user_feedback') || 0;
    wx.request({
      url: app.data.servser + '/index/charge',
      data: {
        session_id: session_id,
        form_id: form_id,
        user_feedback: user_feedback
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 1) {
          wx.showToast({
            title: '还杆成功',
            icon: 'success',
            duration: 2000
          });
          wx.setStorage({
            key: 'money',
            data: res.data.money
          });
          that.setData({ using_info: "hide" });
        } 
      }
    }) 
  },
 
 //跳转我的钱包页面
  goWalletPage:function(){
    if (util.isBindMobile()) {
      wx.navigateTo({  url: '/pages/wallet/wallet'})
    }
  },
  
  //跳转我的红包页面
  goRedPacket:function(){
      if (util.isBindMobile()) {
          wx.navigateTo({ url: '/pages/redpacket/redpacket' })
      }
  },

  //跳转到用户反馈页面
  goFeedbackPage:function(){
     wx.navigateTo({ url: '/pages/feedback/feedback' })
  },

  //用户扫码操作
  doScanCode:function(){
    //判断用户状态是否正常
    var that = this;
    var money = wx.getStorageSync('money');
    //监测该用户是否绑定手机，如果该用户未绑定手机，则auto跳到绑定页
    if(util.isBindMobile()){
      if (money < 0) {
        util.topErrorPrompt(that,0);
      }else{
        wx.scanCode({
          success: (res) => {
            console.log(res);
            var ball_arm_number = util.getQueryString(res.path); 
            if(ball_arm_number){
              wx.redirectTo({ url: '/pages/index/index?query=' + ball_arm_number })
            }else{
              this.setData({
                item: {
                  display: 'show',
                  error_info: '二维码不合法',
                },
              });
              this.closeErrorInfo();
            }
          }
        })
      }
    }
  },

  // getRedPacketInfo: function (session_id) {
  //   var that = this;
  //   wx.request({
  //     url: app.data.servser + '/index/userRedpacket',
  //     data: {
  //       session_id: session_id
  //     },
  //     success: function (res) {

  //       wx.setStorage({
  //         key: 'redpacket_info',
  //         data: res.data
  //       });

  //       wx.setStorage({
  //         key: 'not_used_count',
  //         data: res.data.not_used_count
  //       });
  //     }
  //   })
  // },
  //定时器关闭提示信息
  // closeErrorInfo: function () {
  //   var that = this;
  //   setTimeout(function () { that.setData({ item: { display: 'hide' } }) }, '4000')
  // },
})
